﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Windows.Forms;

using DBMS.Models;
using DBMS.Models.DataInterfaces;
using DBMS.Helpers.Application;
using DBMS.Helpers.Metadata;

namespace DBMS.Data
{
    public class TableRepository : ITableRepository
    {
        private static string DATABASE_DIRECTORY_NAME = "databases";

        public Table GetTable(int databaseId, int tableId)
        {
            SetCurrentDirectoryToAppDir();
            Table table = null;

            //if database is not found, the method should return null or throw an exception

            if (!Directory.Exists(DATABASE_DIRECTORY_NAME + "//" + databaseId + "//" + tableId))
            {
                return table;
            }
            else
            {
                MetadataManager mg = new MetadataManager();
                table = mg.GetTable(databaseId, tableId);
            }

            return table;
        }

        public IList<Table> GetAllTablesForDatabase(int databaseId)
        {
            MetadataManager metaDataManager = new MetadataManager();
            Database db = metaDataManager.GetDatabase(databaseId);
            return db.Tables;
        }

        private void SetCurrentDirectoryToTableDir(int tableId)
        {
            Directory.SetCurrentDirectory(tableId.ToString());
        }

        private void SetCurrentDirectoryToServerDir()
        {
            string currDir = Path.GetDirectoryName(Application.ExecutablePath);
            Directory.SetCurrentDirectory(currDir);
        }

        private void SetCurrentDirectoryToAppDir()
        {
            string appPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
            Directory.SetCurrentDirectory(appPath);
        }

        /// <summary>
        /// Creates a new table on the physical hard drive. It also 
        /// creates the metadata file associated with the database
        /// </summary>
        /// <param name="database">The parent database of the table</param>
        /// <param name="newTable">The new table to be created</param>
        public void Create(Database database, Table newTable)
        {
            foreach (Table table in database.Tables)
            {
                if (table.Name.Equals(newTable.Name))
                {
                    throw new Exception("Duplicate table name for: " + table.Name);
                }
            }

            int newTableIndex = Directory.GetDirectories(Configuration.Instance.GetDatabaseFolder(database)).Length + 1;
            newTable.Index = newTableIndex;

            Directory.CreateDirectory(Configuration.Instance.GetDatabaseFolder(database) + "\\" + newTableIndex.ToString());
            MetadataManager metaDataManager = new MetadataManager();
            metaDataManager.AddTable(database, newTable);
        }

        public void Update(Database database, Table newTable)
        {
            MetadataManager mg = new MetadataManager();
            mg.UpdateTable(database, newTable);
        }

        public void Delete(int databaseId, int tableId)
        {
            MetadataManager metaDataManager = new MetadataManager();
            metaDataManager.DeleteTable(databaseId, tableId);
        }
    }
}
